﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;



namespace Recon.Portal.WebUI
{
    public partial class BankMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }

            if (!IsPostBack)
            {
                //bntbrowse.Enabled = false;
                //bntgetdetails.Enabled = false;
                //rbOption.Focus();
                BindGrid();
            }
        }

        public void BindGrid()
        {
            BankDAO bankDAO = DAOFactory.getBankDAO();
            List<Bank> bankLst = null;

            try
            {
                bankLst = bankDAO.getAllBanks();

                if (bankLst == null)
                {
                    gvCustomer.DataSource = null;
                    gvCustomer.DataBind();
                }
                else
                {
                    gvCustomer.DataSource = bankLst;
                    gvCustomer.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string resp = string.Empty;
            // DBFactory db = new DBFactory();
            // DataTable dtCustomerDetails = new DataTable();
            //BankFacade facade = new BankFacade();

            //if (rbOption.SelectedValue == "1")
            //{

                //  dtCustomerDetails = db.GetCustomerDetailsByBankId(Convert.ToString(txtBankID.Text.Trim()));
                BankDAO bankDAO = DAOFactory.getBankDAO();
                Bank bank = bankDAO.getBank(txtBankID.Text.Trim());
                if (bank != null)
                {
                    bank = new Bank
                    {
                        BankID = bank.BankID,
                        BankName = Convert.ToString(txtName.Text.Trim()),
                        BankIFSC = txtBankIFSC.Text,
                        IsEnabled = chkIsenabled.Checked,
                        ModifiedID = ((User)Session["CurrentUserVO"]).UserID
                    };
                   
                    int retval = bankDAO.updateBank(bank);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Bank details modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error Modifying Bank detail!";
                        btnClickPopup.Show();
                    }
                }
                else
                {
                    bank = new Bank
                    {
                    BankID = Convert.ToString(txtBankID.Text.Trim()),
                    BankName = Convert.ToString(txtName.Text.Trim()),
                    BankIFSC = txtBankIFSC.Text,
                    IsEnabled = chkIsenabled.Checked,
                    EnteredID = ((User)Session["CurrentUserVO"]).UserID
                    };
                    // int retval = db.AddBankDetail(, , , , , , ,,, , , , , , is_Enabled, ((User)Session["CurrentUserVO"]).UserID);
                    int retval = bankDAO.insert(bank);

                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer added successfully!')</script>");
                        lblMessage.Text = "Bank detail added successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Customer!')</script>");
                        lblMessage.Text = "Error adding bank detail!";
                        btnClickPopup.Show();
                    }
                }
                BindGrid();
            /*}
            else
            {
                BankDAO bankDAO = DAOFactory.getBankDAO();
                Bank bank = bankDAO.getBank(txtBankID.Text.Trim());
                // dtCustomerDetails = db.GetCustomerDetailsByBankId(Convert.ToString(txtBankID.Text.Trim()));

                if (bank == null)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer does not exist!')</script>");
                    lblMessage.Text = "Bank does not exist!";
                    btnClickPopup.Show();
                }
                else// if (dtCustomerDetails.Rows.Count == 1)
                {
                    // int retval = db.ModifyBankDetail(Convert.ToString(txtBankID.Text.Trim()), Convert.ToString(txtName.Text.Trim()), Convert.ToString(ddlBankIFSC.SelectedValue), Convert.ToString(txtBankBIC.Text.Trim()), Convert.ToString(txtBranch.Text.Trim()), Convert.ToString(txtAddress1.Text.Trim()), Convert.ToString(txtAddress2.Text.Trim()), Convert.ToString(txtAddress3.Text.Trim()), Convert.ToString(txtCity.Text.Trim()), Convert.ToString(txtState.Text.Trim()), Convert.ToString(txtCountry.Text.Trim()), Convert.ToString(txtZipCode.Text.Trim()), Convert.ToString(txtPhone.Text.Trim()), Convert.ToString(txtEmail.Text.Trim()), is_Enabled, ((User)Session["CurrentUserVO"]).UserID);
                    bank.BankID = bank.BankID;
                    bank.BankName = Convert.ToString(txtName.Text.Trim());
                    bank.BankIFSC = txtBankIFSC.Text;
                    bank.IsEnabled = chkIsenabled.Checked;
                    bank.ModifiedID = ((User)Session["CurrentUserVO"]).UserID;
                    int retval = bankDAO.updateBank(bank);
                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer modified successfully!')</script>");
                        lblMessage.Text = "Bank details modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error Modifying Customer!')</script>");
                        lblMessage.Text = "Error Modifying Bank detail!";
                        btnClickPopup.Show();
                    }

                }

            }*/
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        private void ResetFields()
        {
            //rbOption.SelectedValue = "1";
            //bntgetdetails.Enabled = false;
            //bntbrowse.Enabled = false;
            txtBankID.Text = "";
            txtName.Text = string.Empty;
            txtBankIFSC.Text = string.Empty;
            chkIsenabled.Checked = true;
            btnSubmit.Text = "Add";
        }

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int retval = 0;
            try
            {
                Label bankId = ((Label)gvCustomer.Rows[e.RowIndex].Cells[1].FindControl("lblBankID"));
                Bank bank = new Bank();
                BankDAO bankDAO = DAOFactory.getBankDAO();
                bank.BankID = txtBankID.Text.Trim();
                retval = bankDAO.deleteBank(bank);//userEntity.DeleteUser(Convert.ToString(bankId.Text));
                if (retval != 0)
                {
                    lblMessage.Text = "Bank detail deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting bank detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindGrid();
        }

        //protected void bntbrowse_Click(object sender, EventArgs e)
        //{
        //    string path;
        //    path = "BankMaster.aspx";
        //    // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
        //    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchBank.aspx?textbox=" + HttpUtility.UrlEncode(txtBankID.Text) + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        //}

        //protected void bntgetdetails_Click(object sender, EventArgs e)
        //{

        //    try
        //    {

        //        if (txtBankID.Text.Trim() != "")
        //        {
        //            BankDAO bankDAO = DAOFactory.getBankDAO();
        //            Bank bank = bankDAO.getBank(txtBankID.Text.Trim());
        //            if (bank == null)
        //            {

        //                lblMessage.Text = "No record found for this bank!";
        //                btnClickPopup.Show();
        //            }
        //            else
        //            {
        //                ResetFields();
        //                txtBankID.Text = bank.BankID;
        //                txtName.Text = bank.BankName;
        //                txtBankIFSC.Text = bank.BankIFSC;
        //                chkIsenabled.Checked = bank.IsEnabled;
        //                rbOption.SelectedValue = "2";

        //            }
        //            /*  else
        //              {
        //                  //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('No record found for this customer!')</script>");
        //                  lblMessage.Text = "No record found for this bank!";
        //                  btnClickPopup.Show();
        //              }*/
        //        }
        //        else
        //        {
        //            lblMessage.Text = "Enter Bank ID!";
        //            btnClickPopup.Show();

        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //    }
        //}

        //protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (rbOption.SelectedValue.Trim() == "1")
        //    {
        //        bntbrowse.Enabled = false;
        //        bntgetdetails.Enabled = false;
        //    }
        //    else
        //    {
        //        bntbrowse.Enabled = true;
        //        bntgetdetails.Enabled = true;
        //    }
        //}
    }
}